<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app"></div>
  <script>
    const app = document.getElementById("app")
    // Vue.util.defineProperty(this, 'current','/')
    function defineReactive(obj, key, val) {
      // 属性拦截
      Object.defineProperty(obj, key, {
        get() {
          console.log('get', key)
          return val
        },
        set(newVal) {
          if (newVal != val) {
            console.log('set', key)
            val = newVal
            // 通知视图去更新
            update()
          }
        },
      })
    }
    const obj = {}
    // 响应式处理
    defineReactive(obj, 'foo', 'foo')
    obj.foo = new Date().toLocaleTimeString()

    // 更新函数描述了视图的内容
    // 1.更新函数不应该自己写的：编译器将模板转换出来
    function update() {
      app.innerText = obj.foo
      // todo ................
    }
    setInterval(() => {
      obj.foo = new Date().toLocaleTimeString()
    }, 1000)
  </script>
</body>

</html>